package com.pine.dbant.common.plugin.meta;

import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Locale;

/**
 * @author xiaoyuan
 * @create 2023/2/3 15:53
 **/
public  abstract class AbstractQueryMetadataFunction<C extends MetadataAnalysisConfig,R extends List>  implements QueryMetadataFunction<C ,R>{

    public static  final String CONN_ID="connId";
    public static  final String CATALOG="catalog";
    public static  final String TABLE_NAME="tableName";
    @Getter
    private String type;

    public AbstractQueryMetadataFunction(String type){
        this.type = type;
    }

    @Override
    public R query(C metadataAnalysisConfig, QueryMetadataParam queryMetadataParam) {
        R list =  doQuery(metadataAnalysisConfig,queryMetadataParam);
        if(CollectionUtils.isNotEmpty(list)&&list.get(0) instanceof MetadataNode){
            for(Object o:list){
                MetadataNode metadataNode1 = (MetadataNode) o;
                metadataNode1.addProperty("pluginId",metadataAnalysisConfig.getConnectionInfo().getDsType().toLowerCase(Locale.ROOT));
            }
        }
        return list;
    }

    public abstract R doQuery(C metadataAnalysisConfig, QueryMetadataParam queryMetadataParam);
}
